package com.example.zg.zg_312_ordering_new.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zg.zg_312_ordering_new.R;
import com.example.zg.zg_312_ordering_new.activity.SearchActivity;
import com.example.zg.zg_312_ordering_new.beans.FoodInfo;
import com.example.zg.zg_312_ordering_new.listener.SearchListener;
import com.example.zg.zg_312_ordering_new.model.SearchModel;

import java.io.Serializable;
import java.util.List;

public class SearchFragment extends Fragment {


    private EditText et_search;
    private Button bt_search;
    private String search;
    private List <FoodInfo> foodBeans;


    public SearchFragment(){

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab03,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        et_search=view.findViewById(R.id.editText6);
        bt_search=view.findViewById(R.id.button6);

        bt_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                search=et_search.getText().toString();
                SearchModel searchModel=new SearchModel();
                searchModel.getFoodBySearch(search,searchListener);
            }
        });
    }

    SearchListener searchListener=new SearchListener() {
        @Override
        public void onResponse(List <FoodInfo> foodInfos) {
            foodBeans=foodInfos;
            Intent intent = new Intent(getContext(), SearchActivity.class);
            intent.putExtra("foodBeans", (Serializable) foodBeans);
            startActivity(intent);
        }

        @Override
        public void onFail(String mag) {
            Toast.makeText(getContext(),mag,Toast.LENGTH_SHORT).show();
        }
    };

}